<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" class="tl-dialog" width="1200px" @open="open" @close="close" top="10px" :append-to-body="true" :modal-append-to-body="true">
    <div style="padding-bottom:10px">
      <!-- 查询界面 -->
      <tl-list-search ref="search" :query-param="queryParam" @search="doQuery" :isDisplayDefaultSearch="false" :v-if="false">
        <template slot="search">
          <el-form-item label="发票号码" prop="code">
            <el-input v-model="queryParam.qry_invoiceno" placeholder=""></el-input>
          </el-form-item>
        </template>
      </tl-list-search>
      <!-- 列表界面 -->
      <div>
        <!-- 列表选择 -->
        <div class="table-alert-div" ref="selector">
          <el-alert type="success" :closable="false" class="tl-table-alert-info" effect="light">
            <template slot="title">
              已选择&nbsp;<a class="tl-table-alert-info-a1">{{ selectedRows.length }}</a
              >项&nbsp;&nbsp;
              <a class="tl-table-alert-info-a2" @click="doClearSelected">全部清空</a>
            </template>
          </el-alert>
        </div>
        <!--表格渲染-->
        <el-table class="tl-table" ref="mainTable" :height="tableHeight" v-loading="loading" :data="datas" @selection-change="doSelectChange" @sort-change="doSortChange" @row-dblclick="doRowDbClick" row-key="id" border fit :row-class-name="doChangeRowClassName">
          <el-table-column type="selection" width="55px" fixed="left" v-if="isSingleSelect === false"> </el-table-column>
          <el-table-column prop="dispprojectid" label="项目" width="100px" sortable="custom" sort-by="projectid" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="projectcode" label="项目编号" width="150px" sortable="custom" sort-by="projectcode" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="invoiceapplyid" label="发票申请单ID" width="150px" sortable="custom" sort-by="invoiceapplyid" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="invoiceapplydocno" label="发票申请单据编号" width="150px" sortable="custom" sort-by="invoiceapplydocno" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="invoicedate" label="发票日期" width="170px" sortable="custom" sort-by="invoicedate" align="center" header-align="center" :formatter="dateTimeFormat" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="invoiceno" label="发票号码" width="150px" sortable="custom" sort-by="invoiceno" align="center" header-align="center" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="invoicebillmoney" label="发票金额" width="150px" sortable="custom" sort-by="invoicebillmoney" align="right" header-align="center" :formatter="moneyFormat" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="操作" width="80px" align="center" fixed="right" v-if="isSingleSelect === true">
            <template slot-scope="scope">
              <el-button type="text" icon="el-icon-finished" @click="doSelectData(scope.row[fieldDefaultKey])">选择</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 列表分页 -->
        <el-row class="pagebar" ref="pager">
          <el-pagination @size-change="doChangeSize" @current-change="doChangeIndex" :current-page="pageInfo.currentPage" :page-sizes="pageInfo.pageSizes" :total="pageInfo.totalCount" layout="total, sizes, prev, pager, next, jumper" background class="tl-table-pagination"> </el-pagination>
        </el-row>
      </div>
    </div>
    <div slot="footer" class="dialog-footer" v-if="isSingleSelect === false">
      <el-button type="text" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doConfirm">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_ref_list } from "@tapui/mixins/mixin_ref_list"
/** 子组件 */
/** 工具类 */
import { queryDataCode } from "@tapui/api/dataCode"
import { getAction, putAction, postAction } from "@tapui/utils/http"
/** VUE配置 */
export default {
  name: "TlPmInvoiceRecordRef",
  components: {},
  mixins: [mixin_ref_list],
  data() {
    return {
      description: "项目开票记录参照页面",
      // 默认Key字段名
      fieldDefaultKey: "id",
      // 默认显示字段名
      fieldDefaultValue: "name",
      // url配置
      url: {
        list: "/pm/invoiceRecord/refList"
      },
      // 排序参数
      sorterInfo: {
        sidx: "sortcode",
        sord: "asc"
      }
    }
  },
  methods: {
    /** 多选数据 */
    doConfirm() {
      // 校验选中的发票应该同属于同一个申请
      if (this.selectedRows.length === 0) {
        this.$tap.alert("请先选择发票信息!")
        return
      }
      if (!this.checkFromSameApply()) {
        this.$tap.alert("选择的发票信息必须来源于同一次申请!")
        return
      }
      let selectedValues = ""
      let selectedKeys = ""
      this.selectedRows.forEach((item) => {
        selectedValues += item[this.fieldValueComputed] + ","
      })
      this.selectedRows.forEach((item) => {
        selectedKeys += item[this.fieldKeyComputed] + ","
      })
      if (selectedValues.length > 0) {
        selectedValues = selectedValues.substring(0, selectedValues.length - 1)
      }
      if (selectedKeys.length > 0) {
        selectedKeys = selectedKeys.substring(0, selectedKeys.length - 1)
      }
      this.$emit("confirm", this.selectedRows, selectedKeys, selectedValues)
      this.visible = false
    },
    checkFromSameApply() {
      if (this.selectedRows.length > 1) {
        const applyid = this.selectedRows[0].invoiceapplyid
        for (let i = 1; i < this.selectedRows.length; i++) {
          if (applyid !== this.selectedRows[i].invoiceapplyid) {
            return false
          }
        }
      }
      return true
    }
  }
}
</script>
